#include <iostream>

using namespace std;

class Animal
{
public: 
    virtual void makeSound() = 0; // 纯虚函数，必须进行重写
    virtual ~Animal() {} // 虚析构函数，保证派生类析构正确调用
};

class Dog : public Animal
{
public:
    void makeSound() override
    {
        cout << "汪汪" << endl;
    }
};

class Cat : public Animal
{
public:
    void makeSound() override
    {
        cout << "喵呜" << endl;
    }
};

// 非常方便的进行扩展：
class Bird : public Animal
{
public:
    void makeSound() override
    {
        cout << "叽叽喳喳" << endl;
        return ;
    }
};

void letItSound(Animal* a)
{
    a->makeSound();
    return ;
}

// class person
// {
// public:
//     ~person() -- 析构函数不是虚函数，导致没有调用派生类的析构函数，导致资源泄露
//     {
//         cout << "~person" << endl;
//     }
// };

// class student : public person
// {
// public:
//     ~student() 
//     {
//         cout << "~student" << endl;
//     }
// };

class person
{
public:
    virtual ~person() 
    {
        cout << "~person" << endl;
    }
};

class student : public person
{
public:
    ~student() override
    {
        cout << "~student" << endl;
    }
};

int main()
{
    // Cat cat;
    // Dog dog;
    // Bird bird;
    // letItSound(&cat);
    // letItSound(&dog);
    // letItSound(&bird);

    person* p = new person();
    delete p;

    p = new student();
    delete p;
}